package com.fei.service;

import com.fei.bean.SingerBean;
import com.fei.bean.VideoBean;
import com.fei.mapper.CommentMapper;
import com.fei.mapper.SingerMapper;
import com.fei.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoServiceImpl implements VideoService{

    @Autowired
    SingerMapper singerMapper;
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<SingerBean> selectAllSingerService() {
        return singerMapper.selectAllSinger();
    }

    @Override
    public int videoInsertService(VideoBean bean) {
        return videoMapper.videoInsert(bean);
    }

    @Override
    public int ValidVideoNameService(String video_name) {
        return videoMapper.ValidVideoName(video_name);
    }

    @Override
    public VideoBean selectOneIdVideoService(int id) {
        return videoMapper.selectOneIdVideo(id);
    }

    @Override
    public List<VideoBean> findAllVideoService() {
        List<VideoBean> video = videoMapper.findAllVideo();
        if(video != null){
            for (int i = 0;i<video.size();i++){
                SingerBean singerBean = singerMapper.selectOneId(video.get(i).getSinger_id());
                video.get(i).setSinger_name(singerBean.getName());
            }
        }
        return video;
    }

    @Override
    public int deleteVideoOneIdService(int id) {
        //删除视频对应的评论
        commentMapper.deleteOneVideoId(id);
        return videoMapper.deleteVideoOneId(id);
    }

    @Override
    public int deleteSingerVideoService(int singer_id) {
        List<Integer> videoId = videoMapper.selectOneSingerId(singer_id);
        //删除对应的评论
        if (videoId != null){
            for (int i = 0 ; i < videoId.size() ; i++){
                commentMapper.deleteOneVideoId(videoId.get(i));
            }
        }
        return videoMapper.deleteSingerVideo(singer_id);
    }

    public SingerMapper getSingerMapper() {
        return singerMapper;
    }

    public void setSingerMapper(SingerMapper singerMapper) {
        this.singerMapper = singerMapper;
    }
}
